package com.xneure.database;

import java.util.Map;
import com.xneure.database.sql.GeneralMappingParser;
import com.xneure.database.sql.handler.SqlHandler;
import com.xneure.utils.ObjectUtil;
import com.xneure.utils.bean.GUID;

public class SqlMappingProvider{
	private static GeneralMappingParser parser = new GeneralMappingParser();

	public static GeneralMappingParser getSqlParser(Class<?> cls){
		return parser;
	}

	public String list(Map<?,?> obj){
		Object o = obj.get("param1");
		GeneralMappingParser tmpParser = getSqlParser(o.getClass());
		String query = tmpParser.qurey(o);
		return query;
	}

	public String listByWhere(Object obj,String sql){
		GeneralMappingParser tmpParser = getSqlParser(obj.getClass());
		return tmpParser.qurey(obj,sql);
	}

	public String listByWhere1(Class<?> cls,String where){
		GeneralMappingParser tmpParser = getSqlParser(cls);
		return tmpParser.qureyByWhere(cls,where);
	}

	public String query(Object obj){
		GeneralMappingParser tmpParser = getSqlParser(obj.getClass());
		return tmpParser.qurey(obj);
	}

	public String queryById(Class<?> cls,Object id){
		GeneralMappingParser tmpParser = getSqlParser(cls);
		return tmpParser.qurey(cls,id);
	}

	public String queryByWhere(Object obj,String sql){
		GeneralMappingParser tmpParser = getSqlParser(obj.getClass());
		return tmpParser.qurey(obj,sql);
	}

	public String queryByWhere1(Class<?> cls,String where){
		GeneralMappingParser tmpParser = getSqlParser(cls);
		return tmpParser.qureyByWhere(cls,where);
	}

	public String querySql(Class<?> cls,String sql){
		return sql;
	}

	public String updata(Object obj){
		GeneralMappingParser tmpParser = getSqlParser(obj.getClass());
		return tmpParser.update(obj,null);
	}

	public String executeSql(String sql){
		return sql;
	}

	public String insert(Object obj){
		GeneralMappingParser tmpParser = getSqlParser(obj.getClass());
		return tmpParser.insert(obj);
	}

	public String delete(Object obj){
		GeneralMappingParser tmpParser = getSqlParser(obj.getClass());
		return tmpParser.delete(obj,null);
	}

	public String deleteId(Class<?> cls,GUID... ids){
		if(ObjectUtil.isNull(ids)){
			return null;
		}
		return deleteById(cls,(Object[])ids);
	}

	public String deleteById(Class<?> cls,Object... ids){
		if(ObjectUtil.isNull(ids)){
			return null;
		}
		StringBuilder sb = new StringBuilder("DELETE FROM ");
		sb.append(SqlHandler.getMetaObject(cls).getTableName());
		sb.append(" WHERE ID in (");
		sb.append(SqlHandler.toInSql(ids));
		sb.append(")");
		return sb.toString();
	}
}
